
package cup.modules.visual.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cup.core.mp.base.BaseServiceImpl;
import cup.core.mp.support.Query;
import cup.modules.visual.dto.VisualDTO;
import cup.modules.visual.dto.VisualMapDTO;
import cup.modules.visual.entity.Visual;
import cup.modules.visual.entity.VisualChart;
import cup.modules.visual.entity.VisualConfig;
import cup.modules.visual.mapper.VisualChartMapper;
import cup.modules.visual.mapper.VisualMapper;
import cup.modules.visual.service.IVisualChartService;
import cup.modules.visual.service.IVisualConfigService;
import cup.modules.visual.service.IVisualService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 可视化表 服务实现类
 *
 *
 */
@Service
@AllArgsConstructor
public class VisualChartServiceImpl extends BaseServiceImpl<VisualChartMapper, VisualChart> implements IVisualChartService {


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveChart(VisualChart entity) {
		if (entity!=null && !StringUtils.isEmpty(entity.getName())) {
			LambdaQueryWrapper<VisualChart> visualChartLambdaQueryWrapper=new LambdaQueryWrapper<>();
			visualChartLambdaQueryWrapper.eq(VisualChart::getName,entity.getName());
			Integer count = this.baseMapper.selectCount(visualChartLambdaQueryWrapper);
			if(count>0){
				throw  new RuntimeException("名称不能重复");
			}
		}
		if (ObjectUtils.isNotEmpty(entity.getId())) {
			LambdaQueryWrapper<VisualChart> visualChartLambdaQueryWrapper=new LambdaQueryWrapper<>();
			visualChartLambdaQueryWrapper.eq(VisualChart::getId,entity.getId());
			 VisualChart visualChart = this.baseMapper.selectOne(visualChartLambdaQueryWrapper);
			if (entity.getName()!=null) {
				visualChart.setName(entity.getName());
			}
			if (entity.getType()!=null) {
				visualChart.setType(entity.getType());
			}
			if (entity.getValue()!=null) {
				visualChart.setValue(entity.getValue());
			}
			entity = visualChart;
		}
		this.save(entity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteById(String primaryKey) {
		try {
			this.baseMapper.deleteById(primaryKey);
		} catch (EmptyResultDataAccessException e) {

		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteByIdIn(List<String> ids) {
		this.baseMapper.deleteBatchIds(ids);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public VisualChart findById(String id) {
		return this.baseMapper.selectById(id);
	}

	@Override
	public IPage<VisualChart> findByPage(IPage<VisualChart> page, VisualChart visualChart) {
		LambdaQueryWrapper<VisualChart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.likeRight(VisualChart::getValue,visualChart.getValue());
		lambdaQueryWrapper.likeRight(VisualChart::getType,visualChart.getType());
		lambdaQueryWrapper.likeRight(VisualChart::getName,visualChart.getName());
		return this.baseMapper.selectPage(page,lambdaQueryWrapper);
	}

}
